package com.example.ayniu.treadmill;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ayniu.treadmill.base.BaseActivity;
import com.example.ayniu.treadmill.bean.MemberBean;

import java.util.LinkedList;
import java.util.List;

public class MemberActivity extends BaseActivity implements ListView.OnItemClickListener{

    private ImageView img_menturnback;
    private ListView lv_members;
    private static BaseAdapter members_adapter;
    private List<MemberBean> list_members = new LinkedList<MemberBean>();
    private Button bt_addmember;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member);

        iniView();
    }

    private void iniView(){
/*静态数据，后期应从后台获取数据*/
        MemberBean bean1 = new MemberBean();
        bean1.setName("谢耳朵");
        bean1.setRole("群组成员");
        bean1.setPhone("123456");
        list_members.add(bean1);
        MemberBean bean2 = new MemberBean();
        bean2.setName("超人");
        bean2.setRole("群组成员");
        bean2.setPhone("123456");
        list_members.add(bean2);
/*静态数据，后期应从后台获取数据*/

        img_menturnback = (ImageView)findViewById(R.id.imgmem_turnback);
        img_menturnback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        lv_members = (ListView) findViewById(R.id.lv_members);
        ShowListView();
        lv_members.setAdapter(members_adapter);
        lv_members.setOnItemClickListener(this);

        bt_addmember = (Button) findViewById(R.id.bt_addmember);
        bt_addmember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MemberActivity.this, NewMemberActivity.class);
                intent.putExtra("fromId","add");
                startActivity(intent);
            }
        });
    }

    public void ShowListView(){
        // 实现share的listView列表内容
        members_adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return list_members.size();//
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater nflater = LayoutInflater.from(MemberActivity.this);
                convertView = nflater.inflate(R.layout.layout_lv_members, null);//绑定制定layout文件
                //实例化layout中的控件
                TextView memtv_name = (TextView) convertView.findViewById(R.id.memtv_name);
                TextView memtv_role = (TextView) convertView.findViewById(R.id.memtv_role);
                TextView memtv_phone = (TextView) convertView.findViewById(R.id.memtv_phone);
                memtv_name.setText(list_members.get(position).getName());
                memtv_role.setText(list_members.get(position).getRole());
                memtv_phone.setText(list_members.get(position).getPhone());
                return convertView;
            }
        };
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(MemberActivity.this, NewMemberActivity.class);
        intent.putExtra("fromId","modify");
        intent.putExtra("member_name",list_members.get(position).getName());
        intent.putExtra("member_phone",list_members.get(position).getPhone());
        startActivity(intent);
    }
}
